<template>
	<view class="padding-xl">
		<image class="rounded-circle" :src="userInfo.headimgurl"></image>
		<view class="my-1 text-center">{{userInfo.nickname}}</view>
		<button class="mt-2 block bg-green  lg  cu-btn round" @click="login()">登录</button>
		<button class="mt-2 block bg-orange  lg  cu-btn round" @click="getToken()">获取token</button>
		<button class="mt-2 block bg-orange lg  cu-btn round" @click="getJsapi_ticket()">获取jsapi_ticket</button>
		<button class="mt-2 block bg-orange  lg cu-btn round" @click="getSignature()">获取signature</button>
		<button class="mt-2 block bg-orange  lg cu-btn round" @click="sendMessage">发送模板消息</button>
	</view>
</template>

<script>
	import X2JS from '@/common/x2j/x2js/we-x2js.js';
	export default {
		data() {
			return {
				code: false,
				userInfo:uni.getStorageSync('userInfo')?uni.getStorageSync('userInfo'):false
			}
		},
		onLoad() {
			if(!this.userInfo){
				// 拿着当前的code 去获取openid 顺便获取用户信息
				// this.login()
				
			}
			var body= {
				Content: "132",
				CreateTime: "1608435093",
				FromUserName: "oCyQi6FOnPaBrufZ7gMRRRv36Tgs",
				MsgId: "23027255679979527",
				MsgType: "text",
				ToUserName: "gh_75e4987a7856"
			}
			
			// json转微信专用xml
			let str='<xml>'
			for( let key in body){
				if(key&&body[key]){
					let item=key!=='CreateTime'?`<${key}><![CDATA[${body[key]}]]></${key}>`:`<${key}>${body[key]}</${key}>`
					str+=item
				}
			}
			str+='</xml>'
			console.log('final',str)
			
			
			
			
			var x2js = new X2JS();
			let data=x2js.js2xml(body)
			console.log(data)
			
			
		},
		onShow() {
			let code = this.getQueryString("code")
			if (code&&code!==this.code) {
				this.code = code
				this.getWxLogin()
			}
		},
		methods: {
			sendMessage(){
				uniCloud.callFunction({
						name: 'wxAction',
						data: {
							action: 'send',
							params: {
								openid:this.userInfo.openid,
								nickname:this.userInfo.nickname
							}
						}
					})
					.then(res => {
						console.log(res)
						// console.log(JSON.stringify(res.result.data) )
					})
			},
			// 1、提取微信返回的url的code和state   // ③
			getQueryString(name) {
				let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
				let r = window.location.search.substr(1).match(reg);
				if (r != null) return unescape(r[2]);
				return null;
			},
			// 如·获取code：
			// let code=this.getQueryString("code")
			login() {
				let time=(new Date()).getTime().toString()
				var uri = encodeURIComponent('https://e5aa982f2c5a.ngrok.io/');
				let appid = "wxbd9076b234a875d4"
				const url =
					`https://open.weixin.qq.com/connect/oauth2/authorize?
				appid=${appid}&redirect_uri=${uri}&
				response_type=code&
				scope=snsapi_userinfo&
				state=${time}#wechat_redirect`
				this.code = ''
				window.location.href = url
			},
			// 2、将code传给后端获取相关数据
			getWxLogin() {
				let code = this.getQueryString("code")
				uni.showLoading({
					title:'登录中'
				})
				uniCloud.callFunction({
						name: 'wxAction',
						data: {
							action: 'login',
							params: {
								code
							}
						}
					})
					.then(res => {
						uni.hideLoading()
						console.log(res.result.data)
						if(res.success&&res.result.data&&res.result.status){
							uni.setStorageSync('userInfo',res.result.data)
							this.userInfo=res.result.data
							uni.showToast({
								title:'登录成功',
								icon:'success'
							})
						}else{
							uni.showToast({
								title:'登录失败',
								icon:'none'
							})
						}
					}).catch(err=>{
						uni.hideLoading()
						uni.showToast({
							title:'登录失败',
							icon:'none'
						})
					});
			},
			getSignature() {
				var uri = encodeURIComponent(window.location.href.split('#')[0])
				uniCloud.callFunction({
						name: 'wxAction',
						data: {
							action: 'signature',
							params: {
								url: uri
							}
						}
					})
					.then(res => {
						console.log(res)
					})
			},
			getJsapi_ticket() {
				uniCloud.callFunction({
						name: 'wxAction',
						data: {
							action: 'jsapi_ticket',
							params: {

							}
						}
					})
					.then(res => {
						console.log(res)
						let ticket = res.result.data.ticket
						uni.showModal({
							title: '获得ticket成功',
							content: ticket
						})
					});
			},
			getToken() {
				uniCloud.callFunction({
						name: 'wxAction',
						data: {
							action: 'getToken',
							params: {
								code: 123
							}
						}
					})
					.then(res => {
						let token = res.result.data.token
						uni.showModal({
							title: '获得token成功',
							content: token
						})
					});
			},

			
		}
	}
</script>

<style>

</style>
